package com.tledu.course.controller;

import java.util.List;

import com.tledu.course.domain.SectionManagement;
import com.tledu.course.service.ISectionManagementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.tledu.course.domain.SonSection;
import com.tledu.course.service.ISonSectionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 子章节Controller
 * 
 * @author Ghost
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/course/sonSection")
public class SonSectionController extends BaseController
{
    private String prefix = "course/sonSection";

    private final ISonSectionService sonSectionService;

    // 共用一个构造器
    public SonSectionController(ISonSectionService sonSectionService, ISectionManagementService sectionManagementService) {
        this.sonSectionService = sonSectionService;
        this.sectionManagementService = sectionManagementService;
    }
    // 添加章节逻辑对象
    private final ISectionManagementService sectionManagementService;

    @RequiresPermissions("course:sonSection:view")
    @GetMapping()
    public String sonSection()
    {
        return prefix + "/sonSection";
    }

    /**
     * 查询子章节列表
     */
    @RequiresPermissions("course:sonSection:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SonSection sonSection)
    {
        startPage();
        List<SonSection> list = sonSectionService.selectSonSectionList(sonSection);
        return getDataTable(list);
    }

    /**
     * 导出子章节列表
     */
    @RequiresPermissions("course:sonSection:export")
    @Log(title = "子章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SonSection sonSection)
    {
        List<SonSection> list = sonSectionService.selectSonSectionList(sonSection);
        ExcelUtil<SonSection> util = new ExcelUtil<SonSection>(SonSection.class);
        return util.exportExcel(list, "子章节数据");
    }

    /**
     * 新增子章节
     */
    @GetMapping("/add")
    public String add(ModelMap model)
    {
        // 查询父章节名称
        List<SectionManagement> sectionManagements = sectionManagementService.selectSectionManagementList(null);
        model.addAttribute("fatherSection",sectionManagements);
        return prefix + "/add";
    }

    /**
     * 新增保存子章节
     */
    @RequiresPermissions("course:sonSection:add")
    @Log(title = "子章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SonSection sonSection)
    {

        return toAjax(sonSectionService.insertSonSection(sonSection));
    }

    /**
     * 修改子章节
     */
    @GetMapping("/edit/{sonSectionId}")
    public String edit(@PathVariable("sonSectionId") Integer sonSectionId, ModelMap mmap)
    {
        SonSection sonSection = sonSectionService.selectSonSectionById(sonSectionId);
        mmap.put("sonSection", sonSection);
        return prefix + "/edit";
    }

    /**
     * 修改保存子章节
     */
    @RequiresPermissions("course:sonSection:edit")
    @Log(title = "子章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SonSection sonSection)
    {
        return toAjax(sonSectionService.updateSonSection(sonSection));
    }

    /**
     * 删除子章节
     */
    @RequiresPermissions("course:sonSection:remove")
    @Log(title = "子章节", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sonSectionService.deleteSonSectionByIds(ids));
    }
}
